package com.ap.core.transaction;


/**
 * The Helper Class for generating the AP Transaction Checksum.
 */
public class APTransactionChecksumHelper {
	
	/**
	 * Generate checksum for sequence.
	 * The Checksum is calculated using the Luhn algorithm.
	 * Compute the sum of the digits (Say:67).
	 * Take the units digit (Say:7).
	 * Subtract the units digit from 10.
	 * The result, (Say:3), is the checksum digit.
	 * Note:Since the checksum digit should of be one digit, if checksumDigit >9 return 0
	 *
	 * @param sequenceNumber the sequence number
	 * @return the checksum
	 */
	public static int generateChecksumForSequence(Long sequenceNumber) {
	    int sumOfDigits = computeSumOfDigits(sequenceNumber);
		int checksumDigit = 10-(sumOfDigits%10);
		//Since the checksum digit should of be one digit, if checksumDigit >9 return 0
		return (checksumDigit > 9 ? 0 : checksumDigit);
	}
	


	/**
	 * Compute sum of digits.
	 *
	 * @param input the input
	 * @return the int
	 */
	private static int computeSumOfDigits(Long input) {
		int a=0, sumOfDigits=0;
	       while(input!=0L)
	       {
	    	   
	           a=(int) (input%10);
	           input=input/10;
	           sumOfDigits=sumOfDigits+a;
	       }	       
	       return sumOfDigits;
	}

}
